package net.yc.imalachat.push.media;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.common.app.Activity;
import com.example.common.tools.UiTool;
import com.example.common.widget.GalleryView;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import net.qiujuer.genius.ui.Ui;
import net.yc.imalachat.push.R;

import org.jetbrains.annotations.NotNull;

/**
 * 图片选择Fragment
 */
public class GalleryFragment extends BottomSheetDialogFragment
                            implements GalleryView.SelectedChangedListener{

    private GalleryView mGallery;
    private OnSelectedListener mListener;

    public GalleryFragment() {
    }

    @NonNull
    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        // 返回一个我们复写的
        return new TransStatusBottomSheetDialog(getContext());
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        // 获取我们的galleryView
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        mGallery = root.findViewById(R.id.gallery_view);

        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        mGallery.setup(getLoaderManager(), this);
    }

    @Override
    public void onSelectedCountChanged(int count) {
        if (count > 0) {
            // 如果选中了一张图片，就隐藏自己
            dismiss();
            if (mListener != null) {
                // 得到所有的图片的路径
                String[] path = mGallery.getSelectedPath();
                // 返回第一张
                mListener.onSelectedImage(path[0]);
                // 取消和唤起者之间的引用，加快内存回收
                mListener = null;
            }
        }
    }

    /**
     * 设置事件监听，然后返回自己
     * @param Listener
     * return 自己
     */
    public GalleryFragment setListener(OnSelectedListener Listener) {
        mListener = Listener;
        return this;
    }

    /**
     * 选中图片的监听器
     */
    public interface OnSelectedListener{
        void onSelectedImage(String path);
    }

    /**
     * 为了解决状态栏变黑而写的方法
     */
    public static class TransStatusBottomSheetDialog extends BottomSheetDialog{

        public TransStatusBottomSheetDialog(@NonNull @NotNull Context context) {
            super(context);
        }

        public TransStatusBottomSheetDialog(@NonNull @NotNull Context context, int theme) {
            super(context, theme);
        }

        protected TransStatusBottomSheetDialog(@NonNull @NotNull Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final Window window = getWindow();
            if (window == null) {
                return;
            }

            // 拿到屏幕的高度
            int screenHeight = UiTool.getScreenHeight((Activity) getOwnerActivity());
            // 得到状态栏的高度
            int statusHeight = UiTool.getStatusBarHeight((Activity) getOwnerActivity());

            // 计算dialog的高度并设置
            int dialogHeight = screenHeight - statusHeight;
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight <= 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
        }
    }
}